package ext.acae2.document.mvc.builders;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.doc.WTDocument;
import wt.fc.ReferenceFactory;
import wt.help.HelpContext;
import wt.log4j.LogR;
import wt.util.WTException;
import wt.util.WTMessage;

import com.ptc.core.components.descriptor.DescriptorConstants.ColumnIdentifiers;
import com.ptc.core.htmlcomp.components.ConfigurableTableViewModel;
import com.ptc.core.htmlcomp.tableview.TableViewDescriptor;
import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.jca.mvc.components.JcaTableConfig;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.util.ClientMessageSource;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.misc.NmContext;

import ext.acae2.document.helper.ECMDocHelper;
import ext.acae2.document.resource.DocResource;

/**
 * @desription ECM(工程协调单的列表信息)
 * @author cnie
 * @date 2013-7-15 下午2:38:56
 */

@ComponentBuilder("acae2.mvc.ECMRecommendTableBuilder")
public class ECMRecommendTableBuilder extends AbstractComponentBuilder{
	
	private static final  Logger LOGGER =  LogR.getLogger(ECMRecommendTableBuilder.class.getName());
	
	public static final String RESOURCE=DocResource.class.getName();
	public static final String ECM_REPLY_STATE="ECM_REPLY_STATE"; 
	public static final String REPLY_ECM_NUMBER = "REPLY_ECM_NUMBER";
	
	public static final String PARAM_START_TIME="PARAM_START_TIME";
	public static final String PARAM_END_TIME="PARAM_END_TIME";
	public static final String PARAM_SEND_DEPARTMENT="PARAM_SEND_DEPARTMENT";
	public static final String PARAM_RECEIVE_DEPARTMENT="PARAM_RECEIVE_DEPARTMENT";
	
	

	/* (non-Javadoc)
	 * @see com.ptc.mvc.components.ComponentDataBuilder#buildComponentData(com.ptc.mvc.components.ComponentConfig, com.ptc.mvc.components.ComponentParams)
	 */
	@Override
	public Object buildComponentData(ComponentConfig componentconfig,
			ComponentParams componentparams) throws Exception {
		//List<WTDocument> ecmDocs = new ArrayList<WTDocument>();
		NmContext nmcontext = ((JcaComponentParams)componentparams).getHelperBean().getNmContextBean().getContext();
		NmCommandBean commandBean = ((JcaComponentParams)componentparams).getHelperBean().getNmCommandBean();
		ConfigurableTableViewModel configTableView = (ConfigurableTableViewModel)((JcaComponentParams)componentparams).getDescriptor().getProperties().get("JCA.cachedTableViews");
		TableViewDescriptor currentTableViewDec= configTableView.getActiveView();
		Locale locale = commandBean.getLocale();
		String tableViewName = currentTableViewDec.getName();
		String weiDafuView = WTMessage.getLocalizedMessage(RESOURCE, DocResource.WEIDAFU_VIEW_NAME, null, locale);
		String yudafuView = WTMessage.getLocalizedMessage(RESOURCE, DocResource.YUDAFU_VIEW_NAME, null, locale);
		String wuxudafuView = WTMessage.getLocalizedMessage(RESOURCE, DocResource.WUXUDAFU_VIEW_NAME, null, locale);
		String allView = WTMessage.getLocalizedMessage(RESOURCE, DocResource.ALL_VIEW_NAME, null, locale);
		
		String startTimeStr = commandBean.getTextParameter("null___beginTime_col_beginTime___textbox");
		String endTimestr = commandBean.getTextParameter("null___endTime_col_endTime___textbox");
		String bianzhiDept = commandBean.getTextParameter("null___bianzhiDept___combobox");
		String receiveDept = commandBean.getTextParameter("null___receiveDept___combobox");
		
		Map<String, Object> params = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		sdf.setLenient(false);
		if(StringUtils.isNotBlank(startTimeStr)){
			Timestamp startTime = new Timestamp(sdf.parse(startTimeStr).getTime());
			params.put(PARAM_START_TIME, startTime);
		}
		if(StringUtils.isNotBlank(endTimestr)){
			Timestamp endTime = new Timestamp(sdf.parse(endTimestr).getTime());
			params.put(PARAM_END_TIME, endTime);
		}
		params.put(PARAM_SEND_DEPARTMENT, bianzhiDept);
		params.put(PARAM_RECEIVE_DEPARTMENT, receiveDept);
		
		LOGGER.debug("current view>>>>>>>>>>>>>>>>>>>>>>>>"+tableViewName);
		
		System.out.println(tableViewName);
		Object[] objArr = new Object[2];
		if(weiDafuView.equals(tableViewName)){
			objArr = ECMDocHelper.getNotReplyEcmDoc(params);
		}else if(yudafuView.equals(tableViewName)){
			objArr = ECMDocHelper.getRepliedECMDoc(params);
		}else if(wuxudafuView.equals(tableViewName)){
			objArr = ECMDocHelper.getWUXUDAECMDoc(params);
		}else if(allView.equals(tableViewName)){
			objArr = ECMDocHelper.getALLAvailableEcmDoc(params);
		}
		commandBean.getRequest().getSession().removeAttribute("initECMReplyDocBeans");
		commandBean.getRequest().getSession().setAttribute("initECMReplyDocBeans", objArr[1]);
		return objArr[0];
		
	}

	/* (non-Javadoc)
	 * @see com.ptc.mvc.components.ComponentConfigBuilder#buildComponentConfig(com.ptc.mvc.components.ComponentParams)
	 */
	@Override
	public ComponentConfig buildComponentConfig(ComponentParams componentparams)
			throws WTException {
		
		NmCommandBean commandBean = ((JcaComponentParams)componentparams).getHelperBean().getNmCommandBean();
		
		ComponentConfigFactory factory = getComponentConfigFactory();
		JcaTableConfig tableConfig = (JcaTableConfig) factory.newTableConfig();
		tableConfig.setId("ecm.replydoc.table");
		ClientMessageSource messageResource = getMessageSource(RESOURCE);
		tableConfig.setType(WTDocument.class.getName());
		tableConfig.setConfigurable(true);
		tableConfig.setLabel(messageResource.getMessage(DocResource.CREATE_DOCUMENT_RELEATED_LINK));
		tableConfig.setSelectable(false);
		tableConfig.setShowCount(true);
		tableConfig.setActionModel("ecm doc export toolbar actions");
		//tableConfig.setActionModel("add doclink toolbar actions");
		tableConfig.setInitialRows(true);
		tableConfig.setHelpContext(HelpContext.HELP_SYSTEM);
		ColumnConfig numberColumnConfig =factory.newColumnConfig(ColumnIdentifiers.NUMBER,true);
		numberColumnConfig.setInfoPageLink(true);
		tableConfig.addComponent(numberColumnConfig);
		
		ColumnConfig nameColumnConfig =factory.newColumnConfig(ColumnIdentifiers.NAME,true);
		nameColumnConfig.setInfoPageLink(false);
		tableConfig.addComponent(nameColumnConfig);
		
		ColumnConfig replyECMStateColumnConfig = factory.newColumnConfig(ECM_REPLY_STATE, true);
		replyECMStateColumnConfig.setHidden(true);
		String replyECMState = WTMessage.getLocalizedMessage(RESOURCE, DocResource.ECM_REPLY_STATE, null, commandBean.getLocale());
		replyECMStateColumnConfig.setLabel(replyECMState);
		replyECMStateColumnConfig.setDataUtilityId("ECMReplyDetailDataUtility");
		tableConfig.addComponent(replyECMStateColumnConfig);
		
		ColumnConfig replyECMNumberColumnConfig = factory.newColumnConfig(REPLY_ECM_NUMBER, true);
		replyECMNumberColumnConfig.setHidden(true);
		String replyECMNumber = WTMessage.getLocalizedMessage(RESOURCE, DocResource.REPLY_ECM_NUMBER, null, commandBean.getLocale());
		replyECMNumberColumnConfig.setLabel(replyECMNumber);
		replyECMNumberColumnConfig.setDataUtilityId("ECMReplyDetailDataUtility");
		tableConfig.addComponent(replyECMNumberColumnConfig);
		
		return tableConfig;
	}
	
	

}
